#include "git-compat-util.h"
#include "diff.h"
#include "commit.h"
#include "hash.h"
#include "hash-lookup.h"
#include "hex.h"
#include "patch-ids.h"

/**
 * Checks if a patch ID is defined for the given commit.
 *
 * @param commit Pointer to the commit to check.
 * @return 1 if the commit has a patch ID defined, 0 otherwise.
 */
static int patch_id_defined(struct commit *commit)
{
	/* must be 0 or 1 parents */
	return !commit->parents || !commit->parents->next;
}

/**
 * Calculate and return the patch ID for the given commit.
 *
 * @param commit Pointer to the commit to generate the patch ID for.
 * @param options Pointer to the diff options to use for patch ID generation.
 * @param oid Pointer to store the resulting patch ID object ID.
 * @param diff_header_only Whether to only generate the diff header.
 * @return 0 on success, non-zero on error.
 */
int commit_patch_id(struct commit *commit, struct diff_options *options,
		    struct object_id *oid, int diff_header_only)
{
	if (!patch_id_defined(commit))
		return -1;

	if (commit->parents)
		diff_tree_oid(&commit->parents->item->object.oid,
			      &commit->object.oid, "", options);
	else
		diff_root_tree_oid(&commit->object.oid, "", options);
	diffcore_std(options);
	return diff_flush_patch_id(options, oid, diff_header_only);
}

/*
 * When we cannot load the full patch-id for both commits for whatever
 * reason, the function returns -1 (i.e. return error(...)). Despite
 * the "neq" in the name of this function, the caller only cares about
 * the return value being zero (a and b are equivalent) or non-zero (a
 * and b are different), and returning non-zero would keep both in the
 * result, even if they actually were equivalent, in order to err on
 * the side of safety.  The actual value being negative does not have
 * any significance; only that it is non-zero matters.
 */
/**
 * Compares two patch IDs for inequality. Used as a hash table comparison
 * function.
 *
 * @param cmpfn_data Diff options passed as context.
 * @param eptr The first patch ID hash table entry to compare.
 * @param entry_or_key The second patch ID entry or key to compare against.
 * @param keydata Unused parameter.
 * @return 0 if patch IDs are equal, non-zero if unequal or an error occurs.
 */
static int patch_id_neq(const void *cmpfn_data,
			const struct hashmap_entry *eptr,
			const struct hashmap_entry *entry_or_key,
			const void *keydata UNUSED)
{
	/* NEEDSWORK: const correctness? */
	struct diff_options *opt = (void *)cmpfn_data;
	struct patch_id *a, *b;

	a = container_of(eptr, struct patch_id, ent);
	b = container_of(entry_or_key, struct patch_id, ent);

	if (is_null_oid(&a->patch_id) &&
	    commit_patch_id(a->commit, opt, &a->patch_id, 0))
		return error("Could not get patch ID for %s",
			oid_to_hex(&a->commit->object.oid));
	if (is_null_oid(&b->patch_id) &&
	    commit_patch_id(b->commit, opt, &b->patch_id, 0))
		return error("Could not get patch ID for %s",
			oid_to_hex(&b->commit->object.oid));
	return !oideq(&a->patch_id, &b->patch_id);
}

/**
 * Initializes a patch ID set for the given repository.
 *
 * @param r   The repository to initialize patch IDs for.
 * @param ids The patch ID set to initialize.
 * @return 0 on success, non-zero on failure.
 */
int init_patch_ids(struct repository *r, struct patch_ids *ids)
{
	memset(ids, 0, sizeof(*ids));
	repo_diff_setup(r, &ids->diffopts);
	ids->diffopts.detect_rename = 0;
	ids->diffopts.flags.recursive = 1;
	diff_setup_done(&ids->diffopts);
	hashmap_init(&ids->patches, patch_id_neq, &ids->diffopts, 256);
	return 0;
}

/**
 * Frees memory associated with a patch ID set.
 *
 * @param ids The patch ID set to free.
 * @return 0 on success.
 */
int free_patch_ids(struct patch_ids *ids)
{
	hashmap_clear_and_free(&ids->patches, struct patch_id, ent);
	return 0;
}

/**
 * Initializes a patch ID entry for the given commit.
 *
 * @param patch The patch ID entry to initialize.
 * @param commit The commit the patch ID entry is for.
 * @param ids The patch ID set to add the entry to.
 * @return 0 on success, <0 on failure.
 */
static int init_patch_id_entry(struct patch_id *patch,
			       struct commit *commit,
			       struct patch_ids *ids)
{
	struct object_id header_only_patch_id;

	patch->commit = commit;
	if (commit_patch_id(commit, &ids->diffopts, &header_only_patch_id, 1))
		return -1;

	hashmap_entry_init(&patch->ent, oidhash(&header_only_patch_id));
	return 0;
}

/**
 * Returns the first patch ID entry for the given commit from the patch ID set.
 * Can be used to start iterating over all patch IDs for a commit.
 *
 * @param commit The commit to get the first patch ID for.
 * @param ids    The patch ID set containing patches for the commit.
 * @return The first patch ID entry for the commit, or NULL if none.
 */
struct patch_id *patch_id_iter_first(struct commit *commit,
				     struct patch_ids *ids)
{
	struct patch_id patch;

	if (!patch_id_defined(commit))
		return NULL;

	memset(&patch, 0, sizeof(patch));
	if (init_patch_id_entry(&patch, commit, ids))
		return NULL;

	return hashmap_get_entry(&ids->patches, &patch, ent, NULL);
}

/**
 * Returns the next patch ID entry after the given one from the patch ID set.
 * Can be used to iterate over all patch IDs for a commit.
 *
 * @param cur The current patch ID entry.
 * @param ids The patch ID set to iterate over.
 * @return The next patch ID entry, or NULL if there are no more.
 */
struct patch_id *patch_id_iter_next(struct patch_id *cur,
				    struct patch_ids *ids)
{
	return hashmap_get_next_entry(&ids->patches, cur, ent);
}

/**
 * Returns whether the given commit has an associated patch ID.
 * Checks if patch_id_iter_first() returns a patch ID for the
 * given commit.
 */
int has_commit_patch_id(struct commit *commit,
			struct patch_ids *ids)
{
	return !!patch_id_iter_first(commit, ids);
}

/**
 * Add a patch ID for the given commit to the given patch ID set.
 * Initializes a new patch ID struct, associates it with the commit,
 * computes the patch ID, and adds it to the patch ID hash map.
 * Returns a pointer to the new patch ID struct.
 */
struct patch_id *add_commit_patch_id(struct commit *commit,
				     struct patch_ids *ids)
{
	struct patch_id *key;

	if (!patch_id_defined(commit))
		return NULL;

	CALLOC_ARRAY(key, 1);
	if (init_patch_id_entry(key, commit, ids)) {
		free(key);
		return NULL;
	}

	hashmap_add(&ids->patches, &key->ent);
	return key;
}
